package com.inei.reportedeverificacion.model.dao;

import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.inei.reportedeverificacion.model.connection.UtilDB;
import com.inei.reportedeverificacion.model.entity.Marco;
import com.inei.reportedeverificacion.model.entity.Reporte;

public class MarcoDao implements ICRUD<Marco> {

	// variables
	private UtilDB util;
	private SQLiteDatabase db;
	private Cursor cursor = null;
	private Context context;

	// constructor
	public MarcoDao(Context context) {
		this.context = context;
		util = new UtilDB(context);
	}

	// metodos CRUD
	@Override
	public Marco buscar(Marco o) {
		Marco obj = null;
		db = util.getReadableDatabase();
		cursor = db.rawQuery("select * from Marco where CLAVEVIV=?",
				new String[] { o.getCLAVEVIV() + "" });
		if (cursor.moveToFirst()) {
			obj = new Marco();
			obj.setCLAVEVIV(cursor.getString(cursor.getColumnIndex("CLAVEVIV")));
			obj.setCcdd(cursor.getString(cursor.getColumnIndex("ccdd")));
			obj.setCcpp(cursor.getString(cursor.getColumnIndex("ccpp")));
			obj.setCcdi(cursor.getString(cursor.getColumnIndex("ccdi")));
			obj.setIdgrupo(cursor.getString(cursor.getColumnIndex("idgrupo")));
			obj.setVIVREM(cursor.getString(cursor.getColumnIndex("VIVREM")));
			obj.setCONGLOMERADO(cursor.getString(cursor
					.getColumnIndex("CONGLOMERADO")));
			obj.setNSELV(cursor.getString(cursor.getColumnIndex("NSELV")));
			obj.setC1P17_1A(cursor.getString(cursor.getColumnIndex("C1P17_1A")));
			obj.setC1P17_1B(cursor.getString(cursor.getColumnIndex("C1P17_1B")));
			obj.setC1P17_2A(cursor.getString(cursor.getColumnIndex("C1P17_2A")));
			obj.setC1P17_2B(cursor.getString(cursor.getColumnIndex("C1P17_2B")));
		}
		Cerrar();
		return obj;
	}

	public void borrar(){
		db = util.getWritableDatabase();
		db.execSQL("delete from Marco");
		db.execSQL("delete from Hogar");
		db.execSQL("delete from ReporteTmp");
		db.execSQL("delete from ReporteCap2Tmp");
		db.execSQL("delete from ReporteCap11Tmp");
		db.execSQL("delete from ReporteCap12Tmp");
		Cerrar();
	}
	
	@Override
	public List<Marco> leer() {
		List<Marco> lista = null;
		db = util.getReadableDatabase();
		cursor = db.rawQuery("select * from Marco", null);
		if (cursor.moveToFirst()) {
			lista = new ArrayList<Marco>();
			while (!cursor.isAfterLast()) {
				Marco obj = new Marco();
				obj.setCLAVEVIV(cursor.getString(cursor
						.getColumnIndex("CLAVEVIV")));
				obj.setVIVREM(cursor.getString(cursor.getColumnIndex("VIVREM")));
				obj.setCcdd(cursor.getString(cursor.getColumnIndex("ccdd")));
				obj.setDepartamento(cursor.getString(cursor
						.getColumnIndex("departamento")));
				obj.setCcpp(cursor.getString(cursor.getColumnIndex("ccpp")));
				obj.setProvincia(cursor.getString(cursor
						.getColumnIndex("provincia")));
				obj.setCcdi(cursor.getString(cursor.getColumnIndex("ccdi")));
				obj.setDistrito(cursor.getString(cursor
						.getColumnIndex("distrito")));
				obj.setCONGLOMERADO(cursor.getString(cursor
						.getColumnIndex("CONGLOMERADO")));
				obj.setIdgrupo(cursor.getString(cursor
						.getColumnIndex("idgrupo")));
				obj.setNSELV(cursor.getString(cursor.getColumnIndex("NSELV")));
				obj.setC1P17_1A(cursor.getString(cursor
						.getColumnIndex("C1P17_1A")));
				obj.setC1P17_1B(cursor.getString(cursor
						.getColumnIndex("C1P17_1B")));
				obj.setC1P17_2A(cursor.getString(cursor
						.getColumnIndex("C1P17_2A")));
				obj.setC1P17_2B(cursor.getString(cursor
						.getColumnIndex("C1P17_2B")));
				lista.add(obj);
				cursor.moveToNext();
			}
		}
		Cerrar();
		return lista;
	}

	@Override
	public int crear(Marco o) {
		int resultado = 0;
		db = util.getWritableDatabase();
		db.beginTransaction();
		ContentValues valores = new ContentValues();
		valores.put("CLAVEVIV", o.getCLAVEVIV());
		valores.put("VIVREM", o.getVIVREM());
		valores.put("ccdd", o.getCcdd());
		valores.put("departamento", o.getDepartamento());
		valores.put("ccpp", o.getCcpp());
		valores.put("provincia", o.getProvincia());
		valores.put("ccdi", o.getCcdi());
		valores.put("distrito", o.getDistrito());
		valores.put("CONGLOMERADO", o.getCONGLOMERADO());
		valores.put("idgrupo", o.getIdgrupo());
		valores.put("NSELV", o.getNSELV());
		valores.put("C1P17_1A", o.getC1P17_1A());
		valores.put("C1P17_1B", o.getC1P17_1B());
		valores.put("C1P17_2A", o.getC1P17_2A());
		valores.put("C1P17_2B", o.getC1P17_2B());

		resultado = (int) db.insert("Marco", null, valores);
		if (resultado > 0) {
			db.setTransactionSuccessful();
		}
		valores.clear();
		Cerrar();
		return resultado;
	}

	@Override
	public int eliminar(Marco o) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int actualizar(Marco o) {
		// TODO Auto-generated method stub
		return 0;
	}

	private void Cerrar() {
		if (cursor != null) {
			cursor.close();
		}
		if (db.inTransaction()) {
			db.endTransaction();
		}
		db.close();
	}
}
